{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.datasets import load_digits\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report,confusion_matrix\n",
    "from sklearn import decomposition\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "digits = load_digits()#载入数据\n",
    "x_data = digits.data #数据\n",
    "y_data = digits.target #标签\n",
    "\n",
    "x_train,x_test,y_train,y_test = train_test_split(x_data,y_data) #分割数据1/4为测试数据，3/4为训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,\n",
       "       beta_2=0.999, early_stopping=False, epsilon=1e-08,\n",
       "       hidden_layer_sizes=(100, 50), learning_rate='constant',\n",
       "       learning_rate_init=0.001, max_iter=500, momentum=0.9,\n",
       "       nesterovs_momentum=True, power_t=0.5, random_state=None,\n",
       "       shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1,\n",
       "       verbose=False, warm_start=False)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlp = MLPClassifier(hidden_layer_sizes=(100,50) ,max_iter=500)\n",
    "mlp.fit(x_train,y_train )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       1.00      1.00      1.00        49\n",
      "          1       0.98      0.95      0.96        42\n",
      "          2       1.00      0.96      0.98        52\n",
      "          3       0.95      0.95      0.95        44\n",
      "          4       1.00      1.00      1.00        35\n",
      "          5       0.95      0.97      0.96        40\n",
      "          6       1.00      1.00      1.00        41\n",
      "          7       1.00      0.96      0.98        48\n",
      "          8       0.87      0.95      0.91        43\n",
      "          9       0.96      0.96      0.96        56\n",
      "\n",
      "avg / total       0.97      0.97      0.97       450\n",
      "\n",
      "[[49  0  0  0  0  0  0  0  0  0]\n",
      " [ 0 40  0  0  0  0  0  0  2  0]\n",
      " [ 0  0 50  0  0  1  0  0  1  0]\n",
      " [ 0  0  0 42  0  0  0  0  1  1]\n",
      " [ 0  0  0  0 35  0  0  0  0  0]\n",
      " [ 0  0  0  0  0 39  0  0  0  1]\n",
      " [ 0  0  0  0  0  0 41  0  0  0]\n",
      " [ 0  0  0  1  0  0  0 46  1  0]\n",
      " [ 0  1  0  1  0  0  0  0 41  0]\n",
      " [ 0  0  0  0  0  1  0  0  1 54]]\n"
     ]
    }
   ],
   "source": [
    "predictions = mlp.predict(x_test)\n",
    "print(classification_report(predictions, y_test))\n",
    "print(confusion_matrix(predictions, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(copy=True, iterated_power='auto', n_components=None, random_state=None,\n",
       "  svd_solver='auto', tol=0.0, whiten=False)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca = decomposition.PCA()\n",
    "pca.fit(x_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.78907316e+02, 1.63626641e+02, 1.41709536e+02, 1.01044115e+02,\n",
       "       6.94744827e+01, 5.90756320e+01, 5.18556662e+01, 4.39906130e+01,\n",
       "       4.02885629e+01, 3.69912020e+01, 2.85031708e+01, 2.73059660e+01,\n",
       "       2.18893003e+01, 2.13124899e+01, 1.76269077e+01, 1.69374332e+01,\n",
       "       1.58425689e+01, 1.49961105e+01, 1.22276649e+01, 1.08808010e+01,\n",
       "       1.06876155e+01, 9.57726524e+00, 9.22126826e+00, 8.68553268e+00,\n",
       "       8.36095658e+00, 7.16179198e+00, 6.91588809e+00, 6.18950881e+00,\n",
       "       5.88171633e+00, 5.15299774e+00, 4.48879723e+00, 4.24451468e+00,\n",
       "       4.04518650e+00, 3.94120891e+00, 3.70440987e+00, 3.52968776e+00,\n",
       "       3.08285758e+00, 2.73627648e+00, 2.67062197e+00, 2.54029121e+00,\n",
       "       2.28171700e+00, 1.90618094e+00, 1.81615447e+00, 1.68902395e+00,\n",
       "       1.40119202e+00, 1.29149979e+00, 1.15828926e+00, 9.30701800e-01,\n",
       "       6.69477833e-01, 4.85794730e-01, 2.52210004e-01, 9.90976176e-02,\n",
       "       6.30956535e-02, 6.07039586e-02, 3.96441561e-02, 1.49422438e-02,\n",
       "       8.46835749e-03, 3.62164306e-03, 1.27634048e-03, 6.60902920e-04,\n",
       "       4.11993910e-04, 1.14223098e-30, 1.14223098e-30, 1.12479977e-30])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方差\n",
    "pca.explained_variance_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.48905936e-01, 1.36187712e-01, 1.17945938e-01, 8.40997942e-02,\n",
       "       5.78241466e-02, 4.91691032e-02, 4.31598701e-02, 3.66137258e-02,\n",
       "       3.35324810e-02, 3.07880621e-02, 2.37234084e-02, 2.27269657e-02,\n",
       "       1.82186331e-02, 1.77385494e-02, 1.46710109e-02, 1.40971560e-02,\n",
       "       1.31858920e-02, 1.24813782e-02, 1.01771796e-02, 9.05617439e-03,\n",
       "       8.89538461e-03, 7.97123157e-03, 7.67493255e-03, 7.22903569e-03,\n",
       "       6.95888851e-03, 5.96081458e-03, 5.75614688e-03, 5.15157582e-03,\n",
       "       4.89539777e-03, 4.28887968e-03, 3.73606048e-03, 3.53274223e-03,\n",
       "       3.36683986e-03, 3.28029851e-03, 3.08320884e-03, 2.93778629e-03,\n",
       "       2.56588609e-03, 2.27742397e-03, 2.22277922e-03, 2.11430393e-03,\n",
       "       1.89909062e-03, 1.58652907e-03, 1.51159934e-03, 1.40578764e-03,\n",
       "       1.16622290e-03, 1.07492521e-03, 9.64053065e-04, 7.74630271e-04,\n",
       "       5.57211553e-04, 4.04330693e-04, 2.09916327e-04, 8.24797098e-05,\n",
       "       5.25149980e-05, 5.05243719e-05, 3.29961363e-05, 1.24365445e-05,\n",
       "       7.04827911e-06, 3.01432139e-06, 1.06230800e-06, 5.50074587e-07,\n",
       "       3.42905702e-07, 9.50687638e-34, 9.50687638e-34, 9.36179501e-34])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方差占比\n",
    "pca.explained_variance_ratio_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "variance = []\n",
    "for i in range(len(pca.explained_variance_ratio_)):\n",
    "    variance.append(sum(pca.explained_variance_ratio_[:i+1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHQdJREFUeJzt3Xl0nPV97/H3V/suy9Zi2bIt77aC\n8RJhSFgCBDgGHJZshSRN4kPDzS20vS335pKbhjYkoadJz01yG0JCGiArlJCQuODEpYmBsNh4N95k\ny5IX2dZmW/sympnf/UNjIwvZGtsjPzPP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zyzWz/JOPgZuA7STIe8g51wgcMrO5kVUfBHaSIPlP8XrSf5QP\nNm4B9jA4Z/olr/NEmflp4CgwwOAI4B4G50z/AOyN3I/3OudZ8l/F4P/ybwO2RG63JMprAC4FNkfy\nbwceiqyfAbwF1AK/BDK9zhrFa7kWeCHR8keybo3cdpz8t5so76FI1kXAhsj76DdAUSLld87pCFUR\nET+K52kZERE5Typ3EREfUrmLiPiQyl1ExIdU7iIiPqRyFxHxIZW7iIgPqdxFRHzo/wPUht/4DCHg\ncwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1f86ba3e5f8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(range(1,len(pca.explained_variance_ratio_)+1), variance)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(copy=True, iterated_power='auto', n_components=0.8, random_state=None,\n",
       "  svd_solver='auto', tol=0.0, whiten=True)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca = decomposition.PCA(whiten=True,n_components=0.8)\n",
    "pca.fit(x_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.14890594, 0.13618771, 0.11794594, 0.08409979, 0.05782415,\n",
       "       0.0491691 , 0.04315987, 0.03661373, 0.03353248, 0.03078806,\n",
       "       0.02372341, 0.02272697, 0.01821863])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.explained_variance_ratio_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_train_pca = pca.transform(x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,\n",
       "       beta_2=0.999, early_stopping=False, epsilon=1e-08,\n",
       "       hidden_layer_sizes=(100, 50), learning_rate='constant',\n",
       "       learning_rate_init=0.001, max_iter=500, momentum=0.9,\n",
       "       nesterovs_momentum=True, power_t=0.5, random_state=None,\n",
       "       shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1,\n",
       "       verbose=False, warm_start=False)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlp = MLPClassifier(hidden_layer_sizes=(100,50) ,max_iter=500)\n",
    "mlp.fit(x_train_pca,y_train )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       1.00      1.00      1.00        49\n",
      "          1       0.98      0.95      0.96        42\n",
      "          2       1.00      1.00      1.00        50\n",
      "          3       0.95      0.98      0.97        43\n",
      "          4       1.00      1.00      1.00        35\n",
      "          5       0.98      0.91      0.94        44\n",
      "          6       1.00      1.00      1.00        41\n",
      "          7       1.00      0.98      0.99        47\n",
      "          8       0.91      0.96      0.93        45\n",
      "          9       0.95      0.98      0.96        54\n",
      "\n",
      "avg / total       0.98      0.98      0.98       450\n",
      "\n",
      "[[49  0  0  0  0  0  0  0  0  0]\n",
      " [ 0 40  0  0  0  0  0  0  2  0]\n",
      " [ 0  0 50  0  0  0  0  0  0  0]\n",
      " [ 0  0  0 42  0  0  0  0  1  0]\n",
      " [ 0  0  0  0 35  0  0  0  0  0]\n",
      " [ 0  1  0  0  0 40  0  0  1  2]\n",
      " [ 0  0  0  0  0  0 41  0  0  0]\n",
      " [ 0  0  0  1  0  0  0 46  0  0]\n",
      " [ 0  0  0  1  0  0  0  0 43  1]\n",
      " [ 0  0  0  0  0  1  0  0  0 53]]\n"
     ]
    }
   ],
   "source": [
    "x_test_pca = pca.transform(x_test)\n",
    "predictions = mlp.predict(x_test_pca)\n",
    "print(classification_report(predictions, y_test))\n",
    "print(confusion_matrix(predictions, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
